package controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import service.college.CollegeService;
import service.faculty.FacultyService;
import service.line.LineService;
import service.major.MajorService;
import service.majorunit.MajorunitService;
import service.region.RegionService;
import service.subline.SublineService;
import service.univ.UnivService;
import service.univtype.UnivtypeService;
import util.manager.UploadManager;
import vo.CollegeVO;
import vo.FacultyVO;
import vo.LineVO;
import vo.MajorVO;
import vo.MajorunitVO;
import vo.RegionVO;
import vo.SublineVO;
import vo.UnivVO;

public class MajorContoller extends MultiActionController{
	
	private MajorunitService majorunitService;
	private SublineService sublineService;
	private LineService lineService;
	private RegionService regionService;
	private UnivService univService;
	private CollegeService collegeService;
	private FacultyService facultyService;
	private MajorService majorService;
	private UnivtypeService univtypeService;
	
	public void setMajorunitService(MajorunitService majorunitService) {
		this.majorunitService = majorunitService;
	}
	
	public void setLineService(LineService lineService) {
		this.lineService = lineService;
	}
	
	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
	
	public void setUnivService(UnivService univService) {
		this.univService = univService;
	}
	
	public void setCollegeService(CollegeService collegeService) {
		this.collegeService = collegeService;
	}
	
	public void setFacultyService(FacultyService facultyService) {
		this.facultyService = facultyService;
	}
	
	public void setMajorService(MajorService majorService) {
		this.majorService = majorService;
	}
	
	public void setSublineService(SublineService sublineService) {
		this.sublineService = sublineService;
	}

	public void setUnivtypeService(UnivtypeService univtypeService) {
		this.univtypeService = univtypeService;
	}
	
	public ModelAndView editUniv(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView("major/edit_univ");
		
		mv.addObject("regionlist", regionService.getRegionList());
		mv.addObject("univtypelist", univtypeService.getUnivtypeList());
		
		return mv;
	}
	
	public ModelAndView getUnivList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "univlist", univService.getUnivEditList(request.getParameter("search")));
	}
	
	public ModelAndView registerUniv(HttpServletRequest request, HttpServletResponse response, UnivVO param) throws Exception{
		univService.registerUniv(param);
		return new ModelAndView("redirect:major.do?command=editUniv");
	}
	
	public ModelAndView updateUniv(HttpServletRequest request, HttpServletResponse response, UnivVO param) throws Exception{
		univService.updateUniv(param);
		return new ModelAndView("redirect:major.do?command=editUniv");
	}
	
	public ModelAndView deleteUniv(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		int univ_no=Integer.parseInt(request.getParameter("univ_no"));
		
		UploadManager vo=new UploadManager();
		
		vo.setOldFileName(univService.getUnivInfo(univ_no).getUniv_logo());
		
		deleteLogo(request, response, vo);
		
		univService.deleteUniv(univ_no);
		
		return new ModelAndView("redirect:major.do?command=editUniv");
	}
	
	public ModelAndView registerLogo(HttpServletRequest request, HttpServletResponse response, UploadManager vo) throws Exception{
		
    	String path = getServletContext().getRealPath("img")+"\\logo\\";
    	MultipartFile mFile = vo.getUploadFile();
    	
    	String fileName=System.currentTimeMillis()+"_"+mFile.getOriginalFilename();
    	File f = new File(path+fileName);
    	mFile.transferTo(f);

    	return new ModelAndView("JsonView", "logofile", fileName);
    }

	public ModelAndView deleteLogo(HttpServletRequest request, HttpServletResponse response, UploadManager vo) throws Exception{
		
		String path = getServletContext().getRealPath("img")+"\\logo\\";
		
		 File uf = new File(path + vo.getOldFileName());
		 
         if(uf.exists()) {
             uf.delete();
         }
         
		return null;
	}
	
	public ModelAndView updateLogo(HttpServletRequest request, HttpServletResponse response, UploadManager vo) throws Exception{
		
		deleteLogo(request, response, vo);
		
		return registerLogo(request, response, vo);
	}
	
	public ModelAndView editCollege(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_college");
	}
	
	public ModelAndView getCollegeList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "collegelist", collegeService.getCollegeEditList(request.getParameter("search")));
	}
	
	public ModelAndView registerCollege(HttpServletRequest request, HttpServletResponse response) throws Exception{
		collegeService.registerCollege(request.getParameter("college_name"));
		return new ModelAndView("redirect:major.do?command=editCollege");
	}
	
	public ModelAndView updateCollege(HttpServletRequest request, HttpServletResponse response, CollegeVO param) throws Exception{
		collegeService.updateCollege(param);
		return new ModelAndView("redirect:major.do?command=editCollege");
	}
	
	public ModelAndView deleteCollege(HttpServletRequest request, HttpServletResponse response) throws Exception{
		collegeService.deleteCollege(Integer.parseInt(request.getParameter("college_no")));
		return new ModelAndView("redirect:major.do?command=editCollege");
	}

	public ModelAndView editFaculty(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_faculty");
	}
	
	public ModelAndView getFacultyList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "facultylist", facultyService.getFacultyEditList(request.getParameter("search")));
	}
	
	public ModelAndView registerFaculty(HttpServletRequest request, HttpServletResponse response) throws Exception{
		facultyService.registerFaculty(request.getParameter("faculty_name"));
		return new ModelAndView("redirect:major.do?command=editFaculty");
	}
	
	public ModelAndView updateFaculty(HttpServletRequest request, HttpServletResponse response, FacultyVO param) throws Exception{
		facultyService.updateFaculty(param);
		return new ModelAndView("redirect:major.do?command=editFaculty");
	}
	
	public ModelAndView deleteFaculty(HttpServletRequest request, HttpServletResponse response) throws Exception{
		facultyService.deleteFaculty(Integer.parseInt(request.getParameter("faculty_no")));
		return new ModelAndView("redirect:major.do?command=editFaculty");
	}
	
	public ModelAndView editMajor(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_major");
	}
	
	public ModelAndView getMajorList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "majorlist", majorService.getMajorEditList(request.getParameter("search")));
	}
	
	public ModelAndView registerMajor(HttpServletRequest request, HttpServletResponse response) throws Exception{
		majorService.registerMajor(request.getParameter("major_name"));
		return new ModelAndView("redirect:major.do?command=editMajor");
	}
	
	public ModelAndView updateMajor(HttpServletRequest request, HttpServletResponse response, MajorVO param) throws Exception{
		majorService.updateMajor(param);
		return new ModelAndView("redirect:major.do?command=editMajor");
	}
	
	public ModelAndView deleteMajor(HttpServletRequest request, HttpServletResponse response) throws Exception{
		majorService.deleteMajor(Integer.parseInt(request.getParameter("major_no")));
		return new ModelAndView("redirect:major.do?command=editMajor");
	}
	
	public ModelAndView editMajorunit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_majorunit", "linelist", lineService.getLineList());
	}
	
	public ModelAndView getMajorunitList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "majorunitlist", majorunitService.getMajorunitEditList(request.getParameter("search")));
	}
	
	public ModelAndView registerMajorunit(HttpServletRequest request, HttpServletResponse response, MajorunitVO param) throws Exception{
		majorunitService.registerMajorunit(param);
		return new ModelAndView("redirect:major.do?command=editMajorunit");
	}
	
	public ModelAndView updateMajorunit(HttpServletRequest request, HttpServletResponse response, MajorunitVO param) throws Exception{
		majorunitService.updateMajorunit(param);
		return new ModelAndView("redirect:major.do?command=editMajorunit");
	}
	
	public ModelAndView deleteMajorunit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		majorunitService.deleteMajorunit(Integer.parseInt(request.getParameter("majorunit_no")));
		return new ModelAndView("redirect:major.do?command=editMajorunit");
	}
	
	public ModelAndView getSublineList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView", "sublinelist", sublineService.getSublineListOnLine(Integer.parseInt(request.getParameter("line_no"))));
	}
	
	public ModelAndView editLine(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_line", "linelist", lineService.getLineList());
	}
	
	public ModelAndView registerLine(HttpServletRequest request, HttpServletResponse response) throws Exception{
		lineService.registerLine(request.getParameter("line_name"));
		return new ModelAndView("redirect:major.do?command=editLine");
	}
	
	public ModelAndView updateLine(HttpServletRequest request, HttpServletResponse response, LineVO param) throws Exception{
		lineService.updateLine(param);
		return new ModelAndView("redirect:major.do?command=editLine");
	}
	
	public ModelAndView deleteLine(HttpServletRequest request, HttpServletResponse response) throws Exception{
		lineService.deleteLine(Integer.parseInt(request.getParameter("line_no")));
		return new ModelAndView("redirect:major.do?command=editLine");
	}
	
	public ModelAndView editSubline(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_subline", "linelist", lineService.getLineList());
	}
	
	public ModelAndView registerSubline(HttpServletRequest request, HttpServletResponse response, SublineVO param) throws Exception{
		sublineService.registerSubline(param);
		return new ModelAndView("redirect:major.do?command=editSubline&&line_no="+param.getLine().getLine_no());
	}
	
	public ModelAndView updateSubline(HttpServletRequest request, HttpServletResponse response, SublineVO param) throws Exception{
		sublineService.updateSubline(param);
		return new ModelAndView("redirect:major.do?command=editSubline&&line_no="+param.getLine().getLine_no());
	}
	
	public ModelAndView deleteSubline(HttpServletRequest request, HttpServletResponse response) throws Exception{
		sublineService.deleteSubline(Integer.parseInt(request.getParameter("subline_no")));
		return new ModelAndView("redirect:major.do?command=editSubline&&line_no="+request.getParameter("line_no"));
	}
	
	public ModelAndView editRegion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("major/edit_region", "regionlist", regionService.getRegionList());
	}
	
	public ModelAndView registerRegion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		regionService.registerRegion(request.getParameter("region_name"));
		return new ModelAndView("redirect:major.do?command=editRegion");
	}
	
	public ModelAndView updateRegion(HttpServletRequest request, HttpServletResponse response, RegionVO param) throws Exception{
		regionService.updateRegion(param);
		return new ModelAndView("redirect:major.do?command=editRegion");
	}
	
	public ModelAndView deleteRegion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		regionService.deleteRegion(Integer.parseInt(request.getParameter("region_no")));
		return new ModelAndView("redirect:major.do?command=editRegion");
	}
}
